package com.duan.laugh.upms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.common.security.annotation.Inner;
import com.duan.laugh.common.security.util.UserUtils;
import com.duan.laugh.upms.api.pojo.dto.UserDetailDTO;
import com.duan.laugh.upms.api.pojo.entity.User;
import com.duan.laugh.upms.api.pojo.vo.UserParamsVO;
import com.duan.laugh.upms.service.TenantService;
import com.duan.laugh.upms.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 用户controller
 *
 * @author duanjw
 */
@Slf4j
@RestController
@RequestMapping("user")
@Api(value = "用户管理", tags = "用户")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TenantService tenantService;

    /**
     * 根据用户名、客户端id，包括用户、权限、角色
     *
     * @param userParamsVO 用户参数
     * @return
     */
    @ApiOperation(value = "根据用户名、客户端id、租户查看用户详细信息", notes = "包括用户、权限、角色")
    @PostMapping("detail/user")
    public R<UserDetailDTO> getUserDetailByUserParams(@RequestBody UserParamsVO userParamsVO) {
        final UserDetailDTO userDetailByUser = userService.getUserDetailByUserParams(userParamsVO);
        return R.ok(userDetailByUser);
    }

    @GetMapping("test")
    public Object test(Authentication authentication) {
        return authentication;
    }

    @GetMapping("user")
    public Object user() {
        return UserUtils.getUser();
    }

    @PreAuthorize("hasAnyRole('3')")
    @GetMapping("permission")
    public Object permission() {
        return "你登录了";
    }

    @PreAuthorize("hasAnyRole('1')")
    @GetMapping("permission1")
    public Object permission3() {
        log.info("用户信息：{}", UserUtils.getUser());
        return "你登录了,有角色1";
    }

    @GetMapping("tenant")
    public IPage selectTenant() {
        return tenantService.page(new Page<>());
    }

    /**
     * 根据 user Ids 查询用户基本信息
     *
     * @param params
     * @return
     */
    @Inner
    @PostMapping("/info")
    public R<List<User>> selectInfoByUserIds(@RequestBody Map<String, String> params) {
        String ids = String.valueOf(params.get("ids"));
        return R.ok(userService.selectUsersByReceiveValue(ids));
    }
}
